package com.cyandpos.pad.reprintticket.reprintModel;

import android.content.Context;
import android.text.TextUtils;

import com.changyi.device.printer.PrintManagerLib;
import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.PrintStringList;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.login.configs.GetConfigs;
import com.cyandpos.pad.posdata.O2OPayDetail;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.RefundOrder;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.log.SDTools;
import com.google.gson.JsonObject;



public class ReprintTicketTicketModel implements IReprintTicketModel {
    @Override
    public void askForHttp(QueryOrderReq req, final ReprintTicketDoModelListener listener) {
        PosHttpManager.QueryOrderInfoEexcute(req, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject res) {
                BaseResponse<RefundOrder> baseResponse = BaseResponse.fromJson(res.toString(),RefundOrder.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        TicketInfo ticketInfo = new TicketInfo();
                        for (int i = 0; i < baseResponse.getData().getOrderList().size(); i++) {
                            ticketInfo.setTicket(baseResponse.getData().getOrderList().get(i));
                        }
                        listener.onReprintTickeSuccess(ticketInfo);
                    }catch (Exception e){
                        listener.onReprintTickeFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onReprintTickeFailure(baseResponse.getMsg());
                    }else {
                        listener.onReprintTickeFailure(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onReprintTickeFailure(msg);
            }
        });

    }

    @Override
    public int readFromlocation(int flag, String oldSaleId, Context context) {

        O2OPayDetail o2OPayDetail = SDTools.readO2OTicket(oldSaleId, flag);
        if (o2OPayDetail != null) {

            for(int i = 0; i< GetConfigs.ticketPrintCount(); i++) {
                PrintManagerLib.initPrinter(context, CommonUtils.getSystemModel())
                        .initDevice()
                        .printList(PrintStringList.printO2OList1(o2OPayDetail));
            }
            return 1;
        } else {
            return 0;
        }
    }

    public interface  ReprintTicketDoModelListener{
        void onReprintTickeSuccess(TicketInfo ticketInfo);
        void onReprintTickeFailure(String errorMsg);
    }
}
